//
//  BIDVideoListViewController.m
//  cxgov
//
//  Created by mal on 16/3/15.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDVideoListViewController.h"
#import "BIDVideoCell.h"
#import "BIDWebPageJumpViewController.h"

//视频列表
static NSString *videoListURL = @"videoAndroid!list.action";

@interface BIDVideoListViewController ()<BIDBaseListViewDelegate>
{
    BOOL _bRegister;
}
@end

@implementation BIDVideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"视频";
    // Do any additional setup after loading the view.
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], videoListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld", self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    static NSString *identifier = @"identifier";
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDVideoCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDVideoCell *videoCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    
    videoCell.titleLabel.text = dic[@"title"];
    videoCell.dateLabel.text = [[NSString alloc] initWithFormat:@"上传时间:%@", dic[@"createTime"]];
    videoCell.nameLabel.text = [[NSString alloc] initWithFormat:@"上传人:%@", dic[@"createUser"]];
    [videoCell setReadFlag:YES];
    cell = videoCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    NSString *strURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"htmlUrl"]];
//    BIDWebPageJumpViewController *vc = [[BIDWebPageJumpViewController alloc] initWithNibName:@"BIDWebPageJumpViewController" bundle:nil];
//    vc.desURL = strURL;
//    [self.navigationController pushViewController:vc animated:YES];
    NSURL *url = [NSURL URLWithString:strURL];
    [[UIApplication sharedApplication] openURL:url];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.f;
}

@end
